/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package roomarrange;

import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Calendar;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.model.Option;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version vedioTest.java
 * @version Created on 2009-10-13, 3:15:30
 * @author ben
 */

public class vedioTest extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        t_roomsetDataProvider_vedio.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_roomsetRowSet_vedio}"));
        t_meetingsDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_meetingsRowSet}"));
    }
    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }
    private SingleSelectOptionsList dropDown2DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown2DefaultOptions() {
        return dropDown2DefaultOptions;
    }

    public void setDropDown2DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown2DefaultOptions = ssol;
    }
    private CachedRowSetDataProvider t_roomsetDataProvider_vedio = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_roomsetDataProvider_vedio() {
        return t_roomsetDataProvider_vedio;
    }

    public void setT_roomsetDataProvider_vedio(CachedRowSetDataProvider crsdp) {
        this.t_roomsetDataProvider_vedio = crsdp;
    }
    private IntegerConverter dropDown3Converter = new IntegerConverter();

    public IntegerConverter getDropDown3Converter() {
        return dropDown3Converter;
    }

    public void setDropDown3Converter(IntegerConverter ic) {
        this.dropDown3Converter = ic;
    }
    private Calendar calendar1 = new Calendar();

    public Calendar getCalendar1() {
        return calendar1;
    }

    public void setCalendar1(Calendar c) {
        this.calendar1 = c;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private Calendar calendar2 = new Calendar();

    public Calendar getCalendar2() {
        return calendar2;
    }

    public void setCalendar2(Calendar c) {
        this.calendar2 = c;
    }
    private DropDown dropDown2 = new DropDown();

    public DropDown getDropDown2() {
        return dropDown2;
    }

    public void setDropDown2(DropDown dd) {
        this.dropDown2 = dd;
    }
    private DropDown dropDown3 = new DropDown();

    public DropDown getDropDown3() {
        return dropDown3;
    }

    public void setDropDown3(DropDown dd) {
        this.dropDown3 = dd;
    }
    private CachedRowSetDataProvider t_meetingsDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_meetingsDataProvider() {
        return t_meetingsDataProvider;
    }

    public void setT_meetingsDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_meetingsDataProvider = crsdp;
    }
    private Button button1 = new Button();

    public Button getButton1() {
        return button1;
    }

    public void setButton1(Button b) {
        this.button1 = b;
    }
    private Button button3 = new Button();

    public Button getButton3() {
        return button3;
    }

    public void setButton3(Button b) {
        this.button3 = b;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public vedioTest() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("vedioTest Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
        String app = this.getSessionBean1().getSApp();
        Option[] opt = mainHelper.getTimeQuantum(app);
        this.getDropDown1DefaultOptions().setOptions(opt);
        this.getDropDown2DefaultOptions().setOptions(opt);

    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        if (this.isPostBack()) {
            Object ls_stime = null;
            Object ls_etime = null;
            ls_stime = this.getSessionBean1().getSSdate();
            ls_etime = this.getSessionBean1().getSEdate();
            this.getDropDown1().setValue(ls_stime);
            this.getDropDown2().setValue(ls_etime);
        } else {
            java.util.Date date = new java.util.Date();
            java.util.Date yestoday = new java.util.Date(date.getTime() - 24 * 3600 * 1000);
            this.getCalendar1().setSelectedDate(date);
            this.getCalendar1().setMinDate(yestoday);
            this.getCalendar2().setSelectedDate(date);
            this.getCalendar2().setMinDate(yestoday);
            //一个不存在的条件，为了不刷新出内容
            Object mtId = 0;
            Object sdate = "2109-10-01";
            Object edate = "2109-10-01";

            try {
                CachedRowSet crs = this.getSessionBean1().getT_meetingsRowSet();
                crs.setObject(1, mtId);
                crs.setObject(2, sdate);
                crs.setObject(3, edate);
                crs.execute();
            } catch (SQLException ex) {
                Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
            }
            //判断是否为技术科的人，是才能进行操作
            //获取该会议室的类型
            ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
            DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
            //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
            DataJdbcDao taskDao = new DataJdbcDao();
            taskDao.setDataSource(ds);
            //取会议室类别
            int iusrid=0;
            iusrid=this.getSessionBean1().getIusrid();
            //全体技术科的人都可以
            int li_cnt = taskDao.getGrpid(iusrid, 13,-1);
            if(li_cnt==0){
                //该用户不属于技术科，则将本页2个按钮置为不可用状态
                this.getButton1().setDisabled(true);
                this.getButton3().setDisabled(true);
               }

        }

    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        t_roomsetDataProvider_vedio.close();
        t_meetingsDataProvider.close();
    }

    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String button1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        if (this.getDropDown3().getSelected() == null) {
            info("请选择会议室");
            return null;
        }
        if (this.getCalendar1().getValue() == null) {
            info("请选择开始日期");
            return null;
        }
        if (this.getCalendar2().getValue() == null) {
            info("请选择截止日期");
            return null;
        }
        String ls_sdate = "";
        String ls_edate = "";
        String ls_stime = "";
        String ls_etime = "";
        ls_stime = this.getDropDown1().getValue().toString();
        ls_etime = this.getDropDown2().getValue().toString();
        int li_roomid=0;
        Date ld_sdate = null;
        Date ld_edate = null;
        ld_sdate = (Date) this.getCalendar1().getValue();
        ld_edate = (Date) this.getCalendar2().getValue();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ls_sdate = sdf.format(ld_sdate);
        ls_edate = sdf.format(ld_edate);

        try {

            CachedRowSet crs = this.getSessionBean1().getT_meetingsRowSet();
            crs.setObject(1, this.getDropDown3().getSelected());
            crs.setObject(2, ls_sdate);
            crs.setObject(3, ls_edate);
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.getDropDown1().setValue(ls_stime);
        this.getDropDown2().setValue(ls_etime);
        this.getSessionBean1().setSSdate(ls_stime);
        this.getSessionBean1().setSEdate(ls_etime);
        this.getSessionBean1().setIroomid(li_roomid);
 


        return null;
    }

 
    public String button3_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        //确认申请视频会议
        if (this.getDropDown3().getSelected() == null) {
            info("请选择会议室");
            return null;
        }
        if (this.getCalendar1().getValue() == null) {
            info("请选择开始日期");
            return null;
        }
        if (this.getCalendar2().getValue() == null) {
            info("请选择截止日期");
            return null;
        }
        int li_roomid;
        int li_usrid;

        String ls_sdate = "";
        String ls_edate = "";
        String ls_stime = "";
        String ls_etime = "";
        String ls_room  = "";
        ls_stime = this.getDropDown1().getSelected().toString();
        ls_etime = this.getDropDown2().getSelected().toString();
        this.getSessionBean1().setSSdate(ls_stime);
        this.getSessionBean1().setSEdate(ls_etime);
        li_usrid = this.getSessionBean1().getIusrid();
        Date ld_sdate = null;
        Date ld_edate = null;
        Date ldt_sdate= null;
        Date ldt_edate= null;
        Date ldt_now = null;
        long ll_s=0;
        long ll_e=0;
        long ll_now=0;
        int li_cnt=0;
        ld_sdate = (Date) this.getCalendar1().getValue();
        ld_edate = (Date) this.getCalendar2().getValue();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ls_sdate = sdf.format(ld_sdate);
        ls_edate = sdf.format(ld_edate);
        ls_sdate = ls_sdate + " " + ls_stime + ":00";
        ls_edate = ls_edate + " " + ls_etime + ":00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            ldt_sdate = sdf2.parse(ls_sdate);
            ldt_edate = sdf2.parse(ls_edate);
        } catch (ParseException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        ldt_now = new Date();
        ll_s=ldt_sdate.getTime();
        ll_e=ldt_edate.getTime();
        ll_now=ldt_now.getTime();
        if (ll_s >= ll_e){
            info("开始时间必须早于截止时间");
            return null;

        }
        if (ll_s < ll_now ){
            info("开始时间必须晚于当前时间");
            return null;

        }
        li_roomid = (Integer) this.getDropDown3().getSelected();
        //获取该会议室的类型
        ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
        DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
        //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
        DataJdbcDao taskDao = new DataJdbcDao();
        taskDao.setDataSource(ds);
        //取会议室类别
        int li_roomsort = taskDao.getRoomSort(li_roomid);
        int li_confirmgrpid = taskDao.getRoomConfirmGrpId(li_roomid);
        ls_room = taskDao.getRoomName(li_roomid);
        UUID uuid = UUID.randomUUID();
        String suuid = UUID.randomUUID().toString();//用来生成数据库的主键id
        //将一些变量写入session
        this.getSessionBean1().setIroomid(li_roomid);
        this.getSessionBean1().setSSdate(ls_sdate);
        this.getSessionBean1().setSEdate(ls_edate);
        this.getSessionBean1().setSUUID(suuid);
        this.getSessionBean1().setSRefMtId(ls_room);
        //首先删除临时，或上次未完成的申请
        String sapp = this.getSessionBean1().getSApp();
        taskDao.removeTmp(li_usrid,sapp);
        //增加一个申请
        taskDao.initMeeting(suuid, ls_sdate, ls_edate, li_roomid, li_usrid,li_confirmgrpid,sapp);
        //记录由哪个网页跳到vedioTest2
        this.getSessionBean1().setSRef("vedioTest");


        return "vedio2test";
    }
    
}

